import navHtml from 'ngtemplate!html!../../../tpls/common/navigation.html';
import footerHtml from 'ngtemplate!html!../../../tpls/common/footer.html';

class NavigationController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, getRaceDetail, iRunner, CourseService){

        var vm = this;

        vm.$rootScope    = $rootScope;
        vm.$cookieStore  = $cookieStore;
        vm.$timeout      = $timeout;
        vm.$state        = $state;
        vm.ngDialog      = ngDialog;
        vm.dialogService = dialogService;
        vm.getRaceDetail = getRaceDetail;
        vm.iRunner       = iRunner;
        
        $rootScope.navHtml    = navHtml;
        $rootScope.footerHtml = footerHtml;

        if (vm.getRaceDetail.code == 0) {

            var Detail           = vm.getRaceDetail.data,
                registerNums     = Detail.registerNums,
                racerNums        = Detail.racerNums,
                finishNums       = Detail.finishNums,
                teamRegisterNums = Detail.teamRegisterNums,
                teamRacerNums    = Detail.teamRacerNums,
                teamFinishNums   = Detail.teamFinishNums;

            $rootScope.raceinfo         = Detail;
            $rootScope.isRegNums        = vm.hasNumber(registerNums);
            $rootScope.isRacerNums      = vm.hasNumber(racerNums);
            $rootScope.isFinishNums     = vm.hasNumber(finishNums);
            $rootScope.isTeamRegNums    = vm.hasNumber(teamRegisterNums);
            $rootScope.isTeamRacerNums  = vm.hasNumber(teamRacerNums);
            $rootScope.isTeamFinishNums = vm.hasNumber(teamFinishNums);

        }
        

            CourseService.getCourseList($cookieStore.get('access_token'), vm.getRaceDetail.data.race_id)
            .then(function(data) {
                $rootScope.courseListForNav = data.data;
                console.log($rootScope.courseListForNav);
            })

        
        // 判断是否为赛事主页面
        if ($state.current.name == 'dashboard.race.index') {

            // 初始化复制控件
            new Clipboard('.clipboard');

            // 获取赛事详情
            if (vm.getRaceDetail.code==0) {

                $rootScope.raceinfo   = vm.getRaceDetail.data;
                console.log($rootScope.raceinfo);
                $rootScope.liveHref   = 'http://' + location.host + '/' + vm.getRaceDetail.data.short_name + '/live/index';
                // 查询chegnji
                // http://timing.runningtogether.net/bzzfs/score
                // $rootScope.searchHref = 'http://' + location.host + '/web/race/search?race=' + vm.$state.params.race_id;
                $rootScope.searchHref = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/score';
                //hzyjsxy2016/score/courses
                $rootScope.achievementListHref = 'http://' + location.host + '/' + $rootScope.raceinfo.short_name + '/score/courses';
                // 
                // $rootScope.realtime   = 'http://' + location.host + '/' + vm.getRaceDetail.data.short_name + '/score';
                // $rootScope.rawdata    = 'http://' + location.host + '/' + vm.getRaceDetail.data.short_name + '/scorepanel/rawdata';


                //遍历CP点信息, 处理数据
                // angular.forEach($rootScope.raceinfo.cplist, function(value, index) {
                //     value.PieChart = {
                //         data: [ value.percent, 100-value.percent ],
                //         options: {
                //             fill: ["#1ab394", "#d7d7d7"]
                //         }
                //     }
                // });

                // 赛事直播
                if ($('#qrcode-live').find('canvas').length==0) {
                    $('#qrcode-live').qrcode({
                        width: 100,
                        height: 100,
                        text: $rootScope.liveHref
                    });
                }

                // 成绩查询
                if ($('#qrcode-search').find('canvas').length==0) {
                    $('#qrcode-search').qrcode({
                        width: 100,
                        height: 100,
                        text: $rootScope.searchHref
                    });
                }

                // 成绩查询
                if ($('#qrcode-achievementlist').find('canvas').length==0) {
                    $('#qrcode-achievementlist').qrcode({
                        width: 100,
                        height: 100,
                        text: $rootScope.achievementListHref
                    });
                }
            }

        }

        // 柱状图配置
        $rootScope.barOptions = {
            scaleBeginAtZero : true,
            scaleShowGridLines : true,
            scaleGridLineColor : "rgba(0,0,0,.05)",
            scaleGridLineWidth : 1,
            barShowStroke : true,
            barStrokeWidth : 2,
            barValueSpacing : 5,
            barDatasetSpacing : 1
        };

        // 柱状图数据
        $rootScope.barData = {
            person: {
                labels: ["报名人数", "参赛人数", "完赛人数"],
                datasets: [
                    {
                        label: "My First dataset",
                        fillColor: "rgba(220,220,220,0.2)",
                        strokeColor: "rgba(220,220,220,1)",
                        pointColor: "rgba(220,220,220,1)",
                        pointHighlightStroke: "rgba(220,220,220,1)",
                        data: [registerNums.unknown, racerNums.unknown, finishNums.unknown]
                    },
                    {
                        label: "My First dataset",
                        fillColor: "rgba(181, 184, 207, 0.5)",
                        strokeColor: "rgba(181, 184, 207, 0.8)",
                        pointColor: "rgba(181, 184, 207, 0.75)",
                        pointHighlightStroke: "rgba(181, 184, 207, 1)",
                        data: [registerNums.female, racerNums.female, finishNums.female]
                    },
                    {
                        label: "My Second dataset",
                        fillColor: "rgba(26,179,148,0.5)",
                        strokeColor: "rgba(26,179,148,0.8)",
                        highlightFill: "rgba(26,179,148,0.75)",
                        highlightStroke: "rgba(26,179,148,1)",
                        data: [registerNums.male, racerNums.male, finishNums.male]
                    }
                ]
            },
            team: {
                labels: ["报名队伍数", "参赛队伍数", "完赛队伍数"],
                datasets: [
                    {
                        label: "My First dataset",
                        fillColor: "rgba(220,220,220,0.2)",
                        strokeColor: "rgba(220,220,220,1)",
                        pointColor: "rgba(220,220,220,1)",
                        pointHighlightStroke: "rgba(220,220,220,1)",
                        data: [teamRegisterNums.mixed, teamRacerNums.mixed, teamFinishNums.mixed]
                    },
                    {
                        label: "My First dataset",
                        fillColor: "rgba(181, 184, 207, 0.5)",
                        strokeColor: "rgba(181, 184, 207, 0.8)",
                        pointColor: "rgba(181, 184, 207, 0.75)",
                        pointHighlightStroke: "rgba(181, 184, 207, 1)",
                        data: [teamRegisterNums.female, teamRacerNums.female, teamFinishNums.female]
                    },
                    {
                        label: "My Second dataset",
                        fillColor: "rgba(26,179,148,0.5)",
                        strokeColor: "rgba(26,179,148,0.8)",
                        highlightFill: "rgba(26,179,148,0.75)",
                        highlightStroke: "rgba(26,179,148,1)",
                        data: [teamRegisterNums.male, teamRacerNums.male, teamFinishNums.male]
                    }
                ]
            }
        }

        // 饼图数据
        $rootScope.flotPieData = {
            person: [
                [
                    {
                        label: "未知 / " + registerNums.unknown,
                        data: registerNums.unknown,
                        color: "#d3d3d3"
                    },
                    {
                        label: "女子 / " + registerNums.female,
                        data: registerNums.female,
                        color: "#b5b8cf"
                    },
                    {
                        label: "男子 / " + registerNums.male,
                        data: registerNums.male,
                        color: "#79d2c0"
                    }
                ],
                [
                    {
                        label: "未知 / " + racerNums.unknown,
                        data: racerNums.unknown,
                        color: "#d3d3d3"
                    },
                    {
                        label: "女子 / " + racerNums.female,
                        data: racerNums.female,
                        color: "#b5b8cf"
                    },
                    {
                        label: "男子 / " + racerNums.male,
                        data: racerNums.male,
                        color: "#79d2c0"
                    }
                ],
                [
                    {
                        label: "未知 / " + finishNums.unknown,
                        data: finishNums.unknown,
                        color: "#d3d3d3"
                    },
                    {
                        label: "女子 / " + finishNums.female,
                        data: finishNums.female,
                        color: "#b5b8cf"
                    },
                    {
                        label: "男子 / " + finishNums.male,
                        data: finishNums.male,
                        color: "#79d2c0"
                    }
                ]
            ],
            team: [
                [
                    {
                        label: "混合 / " + teamRegisterNums.mixed,
                        data: teamRegisterNums.mixed,
                        color: "#d3d3d3"
                    },
                    {
                        label: "女子 / " + teamRegisterNums.female,
                        data: teamRegisterNums.female,
                        color: "#b5b8cf"
                    },
                    {
                        label: "男子 / " + teamRegisterNums.male,
                        data: teamRegisterNums.male,
                        color: "#79d2c0"
                    }
                ],
                [
                    {
                        label: "混合 / " + teamRacerNums.mixed,
                        data: teamRacerNums.mixed,
                        color: "#d3d3d3"
                    },
                    {
                        label: "女子 / " + teamRacerNums.female,
                        data: teamRacerNums.female,
                        color: "#b5b8cf"
                    },
                    {
                        label: "男子 / " + teamRacerNums.male,
                        data: teamRacerNums.male,
                        color: "#79d2c0"
                    }
                ],
                [
                    {
                        label: "混合 / " + teamFinishNums.mixed,
                        data: teamFinishNums.mixed,
                        color: "#d3d3d3"
                    },
                    {
                        label: "女子 / " + teamFinishNums.female,
                        data: teamFinishNums.female,
                        color: "#b5b8cf"
                    },
                    {
                        label: "男子 / " + teamFinishNums.male,
                        data: teamFinishNums.male,
                        color: "#79d2c0"
                    }
                ]
            ]
        };

        // 饼图配置
        $rootScope.flotPieOptions = {
            series: {
                pie: {
                    show: true
                }
            },
            grid: {
                hoverable: true
            },
            tooltip: true,
            tooltipOpts: {
                content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
                shifts: {
                    x: 20,
                    y: 0
                },
                defaultTheme: false
            }
        };

    }

    hasNumber(obj) {
        var hasNumber = false;
        if (!angular.isObject(obj)) {
            return false
        }
        for(var i in obj){
            if(obj[i]!==0) {
                hasNumber = true;
                break;
            }
        }
        return hasNumber;
    }
}

NavigationController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'getRaceDetail', 'iRunner', 'CourseService']; 

export default NavigationController;